Find the index of an item of a Tuple¶
T.index(“p”)
Find the index of an item of a Tuple.
# create a tuple
T = tuple("index tuple")
print(T) # ('i', 'n', 'd', 'e', 'x', ' ', 't', 'u', 'p', 'l', 'e')
# get index of the first item whose value is passed as parameter
index = T.index("p")
print(index) # 8
# define the index from which you want to search
index = T.index("p", 5)
print(index) # 8
# define the segment of the tuple to be searched
index = T.index("e", 3, 6)
print(index) # 3
# if item not exists in the tuple return ValueError Exception
index = T.index("y")
Traceback (most recent call last):
File "d0e5ee40-30ab-11e7-a6a0-0b37d4d0b2c6.py", line 14, in <module>
index = T.index("y")
ValueError: tuple.index(x): x not in tuple